package com.rc.activity;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.rc.activity.R;
import com.rc.model.Model;
import com.rc.model.TaskType;
import com.rc.util.Util;
import com.telpoo.frame.model.TaskParams;
import com.telpoo.frame.ui.BaseActivity;

public class SignUp extends BaseActivity implements OnClickListener,
		TextWatcher {

	private EditText txtFirstName, txtLastName, txtEmail, txtPassWord,
			txtConfirmPass;
	private CheckBox cbAgree;
	private Button btSignUp;
	private ScrollView scContent;
	private LinearLayout llProgress;

	public static void callIntent(Context ct) {
		Intent it = new Intent(ct, SignUp.class);
		ct.startActivity(it);
	}

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_sign_up);
		txtFirstName = (EditText) findViewById(R.id.txtItemName);
		txtLastName = (EditText) findViewById(R.id.txtLastName);
		txtEmail = (EditText) findViewById(R.id.txtEmail);
		txtPassWord = (EditText) findViewById(R.id.txtPassWord);
		txtConfirmPass = (EditText) findViewById(R.id.txtConfirmPass);
		cbAgree = (CheckBox) findViewById(R.id.cbAgree);
		btSignUp = (Button) findViewById(R.id.btSignUp);
		llProgress = (LinearLayout) findViewById(R.id.llProgress);
		scContent = (ScrollView) findViewById(R.id.scContent);
		btSignUp.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btSignUp:
			Util.HidenKeyBoard(this);
			if (validate()) {
				callAsynCallServiceRegister(txtFirstName.getText().toString(),
						txtLastName.getText().toString(), txtEmail.getText()
								.toString(), txtPassWord.getText().toString(),
						txtConfirmPass.getText().toString());
			}
			break;

		default:
			break;
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		if (txtFirstName.isFocusable()) {
			Util.setVisiable(txtFirstName);
		} else if (txtLastName.isFocusable()) {
			Util.setVisiable(txtLastName);
		} else if (txtEmail.isFocusable()) {
			Util.setVisiable(txtEmail);
		} else if (txtPassWord.isFocusable()) {
			Util.setVisiable(txtPassWord);
		} else if (txtConfirmPass.isFocusable()) {
			Util.setVisiable(txtConfirmPass);
		}
	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSuccess(int taskType, ArrayList<?> list, String msg) {
		// TODO Auto-generated method stub
		super.onSuccess(taskType, list, msg);
		switch (taskType) {
		case TaskType.TASK_REGISTER:
			SignUpSuceess.callIntent(SignUp.this);
			break;

		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		Login.callIntent(SignUp.this);
		super.onBackPressed();
	}

	@Override
	public void onFail(int taskType, String msg) {
		// TODO Auto-generated method stub
		super.onFail(taskType, msg);

		switch (taskType) {
		case TaskType.TASK_REGISTER:
			showToast(msg);
			break;

		default:
			break;
		}
	}

	private boolean validate() {
		if (!Util.validateText(txtFirstName)) {
			Util.setError(txtFirstName, getString(R.string.input_firstname));
		} else if (!Util.validateText(txtLastName)) {
			Util.setError(txtLastName, getString(R.string.input_lastname));
		} else if (!Util.validateText(txtEmail)) {
			Util.setError(txtEmail, getString(R.string.input_email));
		} else if (!Util.validateEmail(txtEmail)) {
			Util.setError(txtEmail, getString(R.string.invalid_email));
		} else if (!Util.validateText(txtPassWord)) {
			Util.setError(txtPassWord, getString(R.string.input_pass));
		} else if (!Util.validateLeng(txtPassWord)) {
			Util.setError(txtPassWord, getString(R.string.invalid_pass));
		} else if (!Util.validateText(txtConfirmPass)) {
			Util.setError(txtConfirmPass,
					getString(R.string.input_confirm_pass));
		} else if (!Util.validateLeng(txtConfirmPass)) {
			Util.setError(txtConfirmPass,
					getString(R.string.invalid_confirm_pass));
		} else if (!cbAgree.isChecked()) {
			showToast(getString(R.string.you_done_agree));
		} else {
			return true;
		}
		return false;

	}

	/**
	 * call sevirce register user
	 */
	private void callAsynCallServiceRegister(String firstName, String lastName,
			String email, String password, String passwordConfirm) {
		Model mModel = new Model(model, TaskType.TASK_REGISTER, null, this);
		email = email.toLowerCase();
		String[] mParams = { firstName, lastName, email, password,
				passwordConfirm };
		TaskParams params = new TaskParams(mParams);
		if (Util.isNetworkActivated(SignUp.this)) {
			scContent.setVisibility(View.GONE);
			llProgress.setVisibility(View.VISIBLE);
			model.exeTask(params, mModel);
		} else {
			showToast(getString(R.string.no_connection));
		}
	}
}
